﻿#region Using directives

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MCMLXVII.Client.Common.Session;
using MCMLXVII.BackEnd.Services.SystemConfiguration.Messages;
using MCMLXVII.BackEnd.Services.Common.Messages;
using MCMLXVII.BackEnd.Services.OrganizationChart.Interfaces;
using MCMLXVII.BackEnd.Services.OrganizationChart.Messages;
using System.ServiceModel;

#endregion

namespace MCMLXVII.Tools.OrganizationChart
{
    partial class frmURights : Form
    {
        private int _NodeID;
        private bool _BlockInheritance;
        private string _NodeName;
        private int _UpdateSequence;
        private msgOrganizationTreeNodeSecurity sec;

        public bool BlockInheritance
        {
            get { return _BlockInheritance; }
        }
        public int UpdateSequence
        {
            get { return _UpdateSequence; }
        }
        public frmURights( string NodeName, int NodeID, bool blockInheritance, int updateSequence )
        {
            _NodeID = NodeID;
            _NodeName = NodeName;
            _BlockInheritance = blockInheritance;
            _UpdateSequence = updateSequence;

            InitializeComponent();

        }

        private void frmSecurity_Load(object sender, EventArgs e)
        {

            txtFolderName.Text = _NodeID.ToString() + " - " + _NodeName;
            chkBlockInheritance.Checked = _BlockInheritance;


            sec = BusinessActions.GetOrganizationTreeNodeSecurity(_NodeID);
            int MemberID, MemberClass;
            string MemberDescription = "";
            bool FullControl, Read, Delete, Update, Create, NoAccess, ManageSecurity, Audit;
            MemberClass = 0;
            FullControl = false;
            Read = false;
            Update = false;
            Create = false;
            Delete = false;
            NoAccess = false;
            ManageSecurity = false;
            Audit = false;

            if (sec.Inherited != null)
            {
                MemberID = -1;
                foreach (OrganizationTreeNodePermissions per in sec.Inherited)
                {
                    if (per.MemberID != MemberID)
                    {
                        if (MemberID != -1)
                        {
                            gridInherited.Rows.Add(MemberDescription, MemberID, FullControl, Read, Create, Update, Delete, NoAccess, ManageSecurity, Audit);
                        }
                        MemberID = per.MemberID;
                        MemberClass = per.MemberClass;
                        MemberDescription = per.MemberDescription;
                        FullControl = false;
                        Read = false;
                        Update = false;
                        Create = false;
                        Delete = false;
                        NoAccess = false;
                        ManageSecurity = false;
                        Audit = false;
                    }
                    switch (per.Permission)
                    {
                        case "A": Audit = true;
                            break;
                        case "S": ManageSecurity = true;
                            break;
                        case "F": FullControl = true;
                            break;
                        case "N": NoAccess = true;
                            break;
                        case "R": Read = true;
                            break;
                        case "C": Create = true;
                            break;
                        case "U": Update = true;
                            break;
                        case "D": Delete = true;
                            break;
                    }
                }
                if (MemberID != -1)
                {
                    gridInherited.Rows.Add( MemberDescription, MemberID, FullControl, Read, Create, Update, Delete, NoAccess, ManageSecurity, Audit);
                }
            }

            if (sec.Defined != null)
            {
                MemberID = -1;
                foreach (OrganizationTreeNodePermissions per in sec.Defined)
                {
                    if (per.MemberID != MemberID)
                    {
                        if (MemberID != -1)
                        {
                            gridPermissions.Rows.Add(MemberDescription, MemberID, FullControl, Read, Create, Update, Delete, NoAccess, ManageSecurity, Audit);
                        }
                        MemberID = per.MemberID;
                        MemberClass = per.MemberClass;
                        MemberDescription = per.MemberDescription;
                        FullControl = false;
                        Read = false;
                        Update = false;
                        Create = false;
                        Delete = false;
                        NoAccess = false;
                        ManageSecurity = false;
                        Audit = false;
                    }
                    switch (per.Permission)
                    {
                        case "A": Audit = true;
                            break;
                        case "S": ManageSecurity = true;
                            break;
                        case "F": FullControl = true;
                            break;
                        case "N": NoAccess = true;
                            break;
                        case "R": Read = true;
                            break;
                        case "C": Create = true;
                            break;
                        case "U": Update = true;
                            break;
                        case "D": Delete = true;
                            break;
                    }
                }
                if (MemberID != -1)
                {
                    gridPermissions.Rows.Add(MemberDescription, MemberID, FullControl, Read, Create, Update, Delete, NoAccess, ManageSecurity, Audit);
                }
            }

        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            _BlockInheritance = chkBlockInheritance.Checked;
            //Count Nodes                       
            int cant = 0;
            foreach(DataGridViewRow row in gridPermissions.Rows)
            {
                if ((bool)row.Cells["eFullControl"].Value)
                    cant++;
                if ((bool)row.Cells["eRead"].Value)
                    cant++;
                if ((bool)row.Cells["eCreate"].Value)
                    cant++;
                if ((bool)row.Cells["eUpdate"].Value)
                    cant++;
                if ((bool)row.Cells["eDelete"].Value)
                    cant++;
                if ((bool)row.Cells["eNoAccess"].Value)
                    cant++;
                if ((bool)row.Cells["eManageSecurity"].Value)
                    cant++;
                if ((bool)row.Cells["eAudit"].Value)
                    cant++;
            }
            OrganizationTreeNodePermissions[] per = new OrganizationTreeNodePermissions[cant];
            int i = 0;
            int MemberID = 0;
            foreach (DataGridViewRow row in gridPermissions.Rows)
            {
                MemberID = System.Int32.Parse((string)row.Cells["eID"].Value.ToString());
                if ((bool)row.Cells["eFullControl"].Value)
                {
                    per[i] = new OrganizationTreeNodePermissions(MemberID, "", 0, "F");
                    i++;
                }
                if ((bool)row.Cells["eRead"].Value)
                {
                    per[i] = new OrganizationTreeNodePermissions(MemberID, "", 0, "R");
                    i++;
                }
                if ((bool)row.Cells["eCreate"].Value)
                {
                    per[i] = new OrganizationTreeNodePermissions(MemberID, "", 0, "C");
                    i++;
                }
                if ((bool)row.Cells["eUpdate"].Value)
                {
                    per[i] = new OrganizationTreeNodePermissions(MemberID, "", 0, "U");
                    i++;
                }
                if ((bool)row.Cells["eDelete"].Value)
                {
                    per[i] = new OrganizationTreeNodePermissions(MemberID, "", 0, "D");
                    i++;
                }
                if ((bool)row.Cells["eNoAccess"].Value)
                {
                    per[i] = new OrganizationTreeNodePermissions(MemberID, "", 0, "N");
                    i++;
                }
                if ((bool)row.Cells["eManageSecurity"].Value)
                {
                    per[i] = new OrganizationTreeNodePermissions(MemberID, "", 0, "S");
                    i++;
                }
                if ((bool)row.Cells["eAudit"].Value)
                {
                    per[i] = new OrganizationTreeNodePermissions(MemberID, "", 0, "A");
                    i++;
                }
            }
            msgUpdateOrganizationTreeNodeSecurity req = new msgUpdateOrganizationTreeNodeSecurity(_NodeID, _UpdateSequence, _BlockInheritance, per);
            try
            {
                _UpdateSequence =  BusinessActions.UpdateOrganizationTreeNodeSecurity(req);
            }
            catch (FaultException<BackendFault> fault)
            {
                switch (fault.Detail.faultType)
                {
                    case BackendFaulType.Security: MessageBox.Show(Properties.Resources.InsuficientPermissions + " " + fault.Detail.faultDescription );
                        break;
                    case BackendFaulType.DataUpdateSequence: MessageBox.Show(Properties.Resources.ErrorInUpdateSequence + " " + fault.Detail.faultDescription );
                        break;
                    case BackendFaulType.Functional:  MessageBox.Show(Properties.Resources.Exception + " " + fault.Detail.faultDescription );
                        break;
                    case BackendFaulType.Technical:  MessageBox.Show(Properties.Resources.Exception + " " + fault.Detail.faultDescription );
                        break;                
                }
                
            }
            catch (Exception ex)
            {

                MessageBox.Show(string.Format(Properties.Resources.Exception, ex.Message));
            }
            this.DialogResult = DialogResult.OK;            
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void gridPermissions_DoubleClick(object sender, EventArgs e)
        {
            if (gridPermissions.SelectedRows.Count == 1)
            { 
                string MemberName = (string)gridPermissions.SelectedRows[0].Cells["eMemberName"].Value;
                int MemberID = (int)gridPermissions.SelectedRows[0].Cells["eID"].Value;
                bool FullControl = (bool)gridPermissions.SelectedRows[0].Cells["eFullControl"].Value;
                bool Read = (bool)gridPermissions.SelectedRows[0].Cells["eRead"].Value;
                bool Create = (bool)gridPermissions.SelectedRows[0].Cells["eCreate"].Value;
                bool Update = (bool)gridPermissions.SelectedRows[0].Cells["eUpdate"].Value;
                bool Delete = (bool)gridPermissions.SelectedRows[0].Cells["eDelete"].Value;
                bool NoAccess = (bool)gridPermissions.SelectedRows[0].Cells["eNoAccess"].Value;
                bool ManageSecurity = (bool)gridPermissions.SelectedRows[0].Cells["eManageSecurity"].Value;
                bool Audit = (bool)gridPermissions.SelectedRows[0].Cells["eAudit"].Value;

                frmAddPermission frm = new frmAddPermission(MemberID, MemberName, FullControl, Read, Create, Update, Delete, NoAccess, ManageSecurity, Audit);
                if (frm.ShowDialog(this) == DialogResult.OK)
                {
                    gridPermissions.SelectedRows[0].Cells["eMemberName"].Value = frm.MemberName;
                    gridPermissions.SelectedRows[0].Cells["eID"].Value = frm.MemberID;
                    gridPermissions.SelectedRows[0].Cells["eFullControl"].Value = frm.FullControl;
                    gridPermissions.SelectedRows[0].Cells["eRead"].Value = frm.Read;
                    gridPermissions.SelectedRows[0].Cells["eCreate"].Value = frm.Create;
                    gridPermissions.SelectedRows[0].Cells["eUpdate"].Value = frm.UpdateData;
                    gridPermissions.SelectedRows[0].Cells["eDelete"].Value = frm.Delete;
                    gridPermissions.SelectedRows[0].Cells["eNoAccess"].Value = frm.NoAccess;
                    gridPermissions.SelectedRows[0].Cells["eManageSecurity"].Value = frm.ManageSecurity;
                    gridPermissions.SelectedRows[0].Cells["eAudit"].Value = frm.Audit;                
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (gridPermissions.SelectedRows.Count == 1)
            {
                gridPermissions.Rows.Remove(gridPermissions.SelectedRows[0]);
            }

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmAddPermission frm = new frmAddPermission();
            if (frm.ShowDialog(this) == DialogResult.OK)
                gridPermissions.Rows.Add(frm.MemberName , frm.MemberID, frm.FullControl, frm.Read, frm.Create , frm.UpdateData , frm.Delete, frm.NoAccess, frm.ManageSecurity, frm.Audit);
        }

        private void gridPermissions_Click(object sender, EventArgs e)
        {
        
        }

        private void gridInherited_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void gridPermissions_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }
    }
}